package cn.bonoon.kernel.web.handlers;

import cn.bonoon.handler.impl.StandardAutoManager;
import cn.bonoon.kernel.descriptors.ObjectDescriptor;
import cn.bonoon.kernel.support.OperandSupport;
import cn.bonoon.kernel.support.entities.Persistable;
import cn.bonoon.kernel.support.services.GenericService;
import cn.bonoon.kernel.support.services.IService;
import cn.bonoon.kernel.web.controllers.BaseButtonResolver;
import cn.bonoon.kernel.web.controllers.GridButtonResolver;
import cn.bonoon.kernel.web.html.form.FormBuilder;
import cn.bonoon.kernel.web.html.form.StandardUpdateBuilder;

public class DialogUpdateHandler <E extends Persistable> extends AbstractDialogFormableHandler<E>{

	/**
	 * <pre>
	 * 注意，这个构造函数没有自动添加“修改操作”的提交按钮
	 * </pre>
	 * @param register
	 * @param service
	 * @param editClass
	 * @throws Exception 
	 */
	public DialogUpdateHandler(HandlerRegister register, IService<E> service, Class<?> editClass) throws Exception {
		super(register, service, editClass);
	}

	public DialogUpdateHandler(HandlerRegister register, GenericService<E> service, Class<?> editClass) throws Exception {
		this(register, new OperateUpdateHandler<E>(service, register.getManager(), editClass));
	}

	public DialogUpdateHandler(HandlerRegister register, AbstractOperateFormHandler<E> updateHandler) throws Exception {
		super(register, updateHandler.service, updateHandler.editClass);
		defaultButton = updateHandler;
	}

	@Override
	protected FormBuilder builder(ObjectDescriptor objectDescriptor, StandardAutoManager manager) {
		return new StandardUpdateBuilder(objectDescriptor, manager);
	}
	
	@Override
	public BaseButtonResolver register(BaseButtonResolver resolver) throws Exception {
		set("修改信息", true);
		resolver.setName("修改");
		resolver.setKey(OperandSupport.DIALOG_UPDATE);
		return super.register(resolver);
	}
	
	public static <E extends Persistable> GridButtonResolver button(HandlerRegister register, GenericService<E> genericService, Class<?> editClass)throws Exception{
		DialogUpdateHandler<E> duh = new DialogUpdateHandler<E>(register, genericService, editClass);
		GridButtonResolver gbr = register.button();
		duh.register(gbr);
		return gbr;
	}
}